<template>
  <box-card>
    <div slot="operator">
      <el-button icon="el-icon-plus" type="primary" size="mini" @click="handleAdd">新增</el-button>
      <el-button :disabled="multiple" icon="el-icon-delete" type="danger" size="mini" @click="handleDelete">删除
      </el-button>
    </div>
    <el-form slot="search" ref="queryForm" :inline="true" :model="queryParams" class="search-form"
             label-position="left" label-suffix="：" size="small">
      <el-form-item label="网址名称" prop="name">
        <el-input v-model="queryParams.name" clearable placeholder="请输入网址名称"
                  @keyup.enter.native="handleQuery"/>
      </el-form-item>
      <el-form-item>
        <el-button icon="el-icon-search" size="mini" type="primary" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" type="info" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>
    <el-table slot="content" v-loading="loading" :data="dataList" class="table-container"
              @selection-change="handleSelectionChange">
      <el-table-column align="center" type="selection" width="55"/>
      <el-table-column label="名称" min-width="100px" prop="name" show-overflow-tooltip/>
      <el-table-column label="地址" min-width="150px" prop="link" show-overflow-tooltip/>
      <el-table-column label="备注" min-width="120px" prop="remark" show-overflow-tooltip/>
      <el-table-column class-name="small-padding fixed-width" fixed="right" label="操作" min-width="150">
        <template v-if="scope.row.id !== 1" v-slot:default="scope">
          <el-button icon="el-icon-s-promotion" size="mini" type="text" @click="handleLink(scope.row)">访问
          </el-button>
          <el-button icon="el-icon-edit" size="mini" type="text" @click="handleEdit(scope.row)">修改
          </el-button>
          <el-button slot="reference" icon="el-icon-delete" size="mini" type="text" @click="handleDelete(scope.row)">
            删除
          </el-button>
        </template>
      </el-table-column>
    </el-table>
    <pagination slot="page" v-show="pageParam.total > 0" :limit.sync="pageParam.pageSize" :page.sync="pageParam.pageNum"
                :total="pageParam.total" @pagination="getList"/>
    <el-dialog :title="dialog.title" :visible.sync="dialog.open" append-to-body width="400px" @confirm="handleSubmit">
      <el-form ref="form" :model="formdata" :rules="rules" label-suffix="：" label-width="80px">
        <el-form-item label="名称" prop="name">
          <el-input v-model="formdata.name" placeholder="请输入名称"/>
        </el-form-item>
        <el-form-item label="地址" prop="link">
          <el-input v-model="formdata.link" placeholder="请输入地址"/>
        </el-form-item>
        <el-form-item label="备注" prop="remark">
          <el-input v-model="formdata.remark" placeholder="请输入内容" type="textarea"/>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="handleSubmit">确 定</el-button>
        <el-button @click="handleCancel">取 消</el-button>
      </div>
    </el-dialog>
  </box-card>
</template>

<script>
import {getOne, list, remove, save} from "@/api/base";

export default {
  name: "WebsiteCard",
  props: {
    userId: String,
  },
  data() {
    return {
      // 遮罩层
      loading: true,
      baseUrl: "/system/website/",
      id: undefined,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      dataList: [],
      queryParams: {},
      pageParam: {
        pageNum: 1,
        pageSize: 10,
        total: 0,
      },
      dialog: {
        open: false,
        title: '',
      },
      formdata: {},
      rules: {}
    }
  },
  created() {
    if (this.userId === undefined) {
      console.log(this.$store.getters.user.id)
      this.userId = this.$store.getters.user.id
    }
    this.resetQuery();
  },
  methods: {
    getList() {
      this.loading = true;
      list(this.baseUrl, {
        ...this.queryParams,
        userId: this.userId,
        pageNum: this.pageParam.pageNum,
        pageSize: this.pageParam.pageSize
      }).then((response) => {
        this.dataList = response.rows;
        this.pageParam.total = response.total;
        this.loading = false;
      });
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.pageParam.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map((item) => item.id);
      this.single = selection.length !== 1;
      this.multiple = !selection.length;
    },
    handleLink(row) {
      window.open(`${row.link}`, '_blank');
    },
    handleAdd() {
      this.resetForm("form")
      this.dialog.open = true;
      this.dialog.title = '新增功能'
    },
    handleEdit(row) {
      getOne(this.baseUrl, row.id).then(response => {
        this.formdata = response.data;
        this.dialog.open = true;
        this.dialog.title = '修改功能'
      })
    },
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除选择的网址信息？').then(() => {
        remove(this.baseUrl, ids, ids.length > 1).then(() => {
          this.getList();
          this.$modal.msgSuccess("删除成功");
        }).catch(() => {
        })
      })
    },
    handleSubmit() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          save(this.baseUrl, this.formdata).then(() => {
            this.$modal.msgSuccess("保存成功");
            this.dialog.open = false;
            this.getList();
          });
        }
      });
    },
    handleCancel() {
      this.formdata = {userId: this.userId};
      this.dialog.open = false;
    }
  }
}

</script>
<style scoped lang="scss">

</style>
